import request from '@/plugin/axios'
import { ApiEncrypt } from '../../src/api/cipherHelper.js'
// 读取二进制文件,或者url
export function ReadBinaryFile (filename) {
  var oReq = new XMLHttpRequest()
  oReq.open('GET', filename, false)
  oReq.overrideMimeType('text/plain; charset=x-user-defined')
  oReq.send(null)
  if (oReq.status !== 200) return ''
  return (oReq.responseText)
}
// 读取文本文件
export function ReadTextFile (filename) {
  var oReq = new XMLHttpRequest()
  oReq.open('GET', filename, false)
  oReq.overrideMimeType('text/plain; charset=utf8')
  oReq.send(null)
  if (oReq.status !== 200) return ''
  return (oReq.responseText)
}
// 读文本文件测试
export function ReadTextFileTest () {
  var filename = '../static/test.txt'
  var filestream = ReadTextFile(filename)
  console.log(filestream)
}
// 读二进制文件测试
export function ReadBinaryFileTest () {
  var filename = '../static/queueInfo.db'
  var filestream = ReadBinaryFile(filename)
  console.log(filestream)
}
// getTableData接口测试
export function GetTableDataTest () {
  var apiParam = {
    mainUrl: 'http://api.legendleader.com/index.php/api/rms/',
    tableName: 'rms_printer',
    methodName: 'getTableData',
    key: '',
    value: '',
    SQL: '',
    jsonString: ''
  }
  var postReturn = ''
  GetApiData(apiParam).then(res => {
    postReturn = res
    console.log('getTableData接口返回content数据')
    console.log(res)
  })
    .catch(err => {
      console.log(err)
    })
  return postReturn
}
// getTableDataByParam接口测试
export function GetTableDataByParamTest () {
  var apiParam = {
    mainUrl: 'http://api.legendleader.com/index.php/api/rms/',
    tableName: 'rms_printer',
    methodName: 'getTableDataByParam',
    key: 'id',
    value: '1',
    SQL: '',
    jsonString: ''
  }
  var postReturn = ''
  GetApiData(apiParam).then(res => {
    postReturn = res
    console.log('getTableDataByParam接口返回content数据')
    console.log(res)
  })
    .catch(err => {
      console.log(err)
    })
  return postReturn
}
// getDataBySQL接口测试
export function GetTableDataBySQLTest () {
  var apiParam = {
    mainUrl: 'http://api.legendleader.com/index.php/api/rms/',
    tableName: 'rms_printer',
    methodName: 'getDataBySQL',
    key: 'id',
    value: '1',
    SQL: 'select * from rms_printer',
    jsonString: ''
  }
  var postReturn = ''
  GetApiData(apiParam).then(res => {
    postReturn = res
    console.log('getTableDataBySQL接口返回content数据')
    console.log(res)
  })
    .catch(err => {
      console.log(err)
    })
  return postReturn
}
// updateTableData接口测试
export function UpdateTableDataTest () {
  var jsonObject = {
    id: '1',
    name3: 'uve-cli'
  }
  var jsonUpdateString = JSON.stringify(jsonObject)
  var apiParam = {
    mainUrl: 'http://api.legendleader.com/index.php/api/rms/',
    tableName: 'rms_printer',
    methodName: 'updateTableData',
    key: 'id',
    value: '1',
    SQL: 'select * from rms_printer',
    jsonString: jsonUpdateString
  }
  var postReturn = ''
  GetApiData(apiParam).then(res => {
    postReturn = res
    console.log('updateTableData接口更新数据')
    console.log(res)
  })
    .catch(err => {
      console.log(err)
    })
  return postReturn
}
// 得到api接口Post所需要的Post参数数据
// getTableData 用到的属性tableName,methodName
// getTableDataByParam 用到的属性tableName,key,value,methodName
// getDataBySQL 用到的属性SQL,methodName
// updateTableData 用到的属性tableName,key,value,jsonString,methodName
export function GetCParamOfPostData (apiParam) {
  var cParam = ''
  if (apiParam.methodName === 'getTableData') {
    cParam = 'tablename=' + apiParam.tableName + '&method=common.' + apiParam.methodName
  } else if (apiParam.methodName === 'getTableDataByParam') {
    cParam = 'tablename=' + apiParam.tableName + '&key=' + apiParam.key + '&value=' + apiParam.value + '&method=common.' + apiParam.methodName
  } else if (apiParam.methodName === 'getDataBySQL') {
    cParam = 'SQL=' + apiParam.SQL + '&method=common.' + apiParam.methodName
  } else if (apiParam.methodName === 'updateTableData') {
    // tablename={0}&key={1}&value={2}&data={3}
    cParam = 'tablename=' + apiParam.tableName + '&key=' + apiParam.key + '&value=' + apiParam.value + '&data=' + apiParam.jsonString + '&method=common.' + apiParam.methodName
  }
  return cParam
}
// 得到api接口Post所需要的Post参数数据
// 用到的属性tableName,method
export function GetPostDataOfApi (apiParam) {
  var postData = ''
  // 表名参数
  var cParam = GetCParamOfPostData(apiParam)
  // 参数错误
  if (cParam.length === 0) return postData
  // 时间戳参数
  var timeStamp = new Date().Format('yyyyMMddhhmmss')
  var timeStampParam = '&timestamp=' + timeStamp
  // 加密签名参数
  var encryptSign = ApiEncrypt('common.' + apiParam.methodName + timeStamp)
  var encryptSignParam = '&sign=' + encryptSign
  // post数据
  postData = cParam + encryptSignParam + timeStampParam
  return postData
}
// 得到api接口数据(数据表的全部数据 getTableData)
// getTableData 用到的属性mainUrl,tableName,methodName
// getTableDataByParam 用到的属性mainUrl,mainUrl,tableName,key,value,methodName
// getDataBySQL 用到的属性mainUrl,SQL,methodName
// updateTableData 用到的属性mainUrl,tableName,key,value,jsonString,methodName
export function GetApiData (apiParam) {
  // 主URL
  var mainUrl = apiParam.mainUrl
  // post数据
  var postData = GetPostDataOfApi(apiParam)
  console.log(postData)
  return request({
    url: mainUrl,
    method: 'post',
    responseType: 'json',
    data: postData
  })
}
// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
// eslint-disable-next-line no-extend-native
Date.prototype.Format = function (fmt) {
  var o = {
    'M+': this.getMonth() + 1,
    'd+': this.getDate(),
    'h+': this.getHours(),
    'm+': this.getMinutes(),
    's+': this.getSeconds(),
    'q+': Math.floor((this.getMonth() + 3) / 3),
    'S': this.getMilliseconds()
  }
  if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)) }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
  }
  return fmt
}
// 格式化时间
export function formatTimeToStr (times, pattern) {
  var d = new Date(times).Format('yyyyMMddhhmmss')
  if (pattern) {
    d = new Date(times).Format(pattern)
  }
  return d.toLocaleString()
}
export function GetData (data) {
  return request({
    url: '/api/getdata',
    method: 'post',
    data
  })
}
export function GetDataBySQL (data) {
  return request({
    url: '/api/getdatabysql',
    method: 'post',
    data: data
  })
}
export function SetData (data) {
  return request({
    url: '/api/setdata',
    method: 'post',
    data
  })
}
export function DeleData (data) {
  return request({
    url: '/api/deledata',
    method: 'post',
    data
  })
}
export function SetDispord (data) {
  return request({
    url: '/api/setdispord',
    method: 'post',
    data
  })
}
export function ExecuteSql (data) {
  return request({
    url: '/api/executeSql',
    method: 'post',
    data
  })
}
export function Upload (data) {
  return request({
    url: '/api/upload',
    method: 'post',
    data
  })
}
export function InitOutletPrinter (data) {
  return request({
    url: '/api/init_outlet_printer',
    method: 'post',
    data
  })
}
// 更新系統設定
export function UpdateShopSetting (data) {
  return request({
    url: '/api/update_shop_setting',
    method: 'post',
    data
  })
}
// 更新數據表（一行或多行記錄)
export function UpdateTable (data) {
  return request({
    url: '/api/updatetable',
    method: 'post',
    data
  })
}
